package com.example.sikong.service;

import com.example.common.RedisKeyName;
import com.example.sikong.dto.LiveContainerResultDTO;
import com.example.sikong.dto.SiKongResult;
import com.example.sikong.feign.SiKongApiClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@EnableAsync
public class SiKongSyncService {
    @Autowired
    private SiKongApiClient siKongApiClient;

    @Autowired
    private RedisProvider redisProvider;

    /**
     * 查询直播是否在线,并把直播信息存储到redis
     */
    @Async
    public void refreshInLive(String orgSecret, String sn, String camera) {
        try {
            SiKongResult<LiveContainerResultDTO> liveContainer = siKongApiClient.getLiveContainer(orgSecret, String.join("_", sn, camera));
            LiveContainerResultDTO data = liveContainer.getData();
            if (data == null || data.getTotal_count() == 0) {
                return;
            }
            List<LiveContainerResultDTO.ContainerDTO> members = data.getMembers();
            for (LiveContainerResultDTO.ContainerDTO member : members) {
                if (member.isValidate()) {
                    member.setOrgSecret(orgSecret);
                    redisProvider.set(RedisKeyName.SI_KONG_IN_LIVE + ":" + sn + ":" + member.getConverter_id(), member);
                    continue;
                }
                try {
                    // 如果不等于200 直接当时就关闭了
                    siKongApiClient.stopLive(orgSecret, member.getConverter_id());
                    redisProvider.del(RedisKeyName.SI_KONG_IN_LIVE + ":" + sn + ":" + member.getConverter_id());
                } catch (Exception e) {
                    log.warn("删除无用直播失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
        }
    }
//    @Async
//    public void refreshInLive(String orgSecret, String sn, String camera) {
//        SiKongStartLiveDTO params = new SiKongStartLiveDTO();
//        params.setSn(sn);
//        params.setCamera(camera);
//        SiKongResult<LiveStartResultDTO> liveContainer = siKongApiClient.startLive(orgSecret, params);
//
//        LiveStartResultDTO data = liveContainer.getData();
//        LiveStartResultDTO.LiveStartDTO liveStartDTO = data.getData();
//        if (data == null) {
//            return;
//        }
//        if (liveContainer.getCode() == 0) {
//            redisProvider.set(RedisKeyName.SI_KONG_IN_LIVE + ":" + sn + ":" + liveStartDTO.getConverter_id(), liveStartDTO);
//        } else {
//            try {
//                // 如果不等于200 直接当时就关闭了
//                siKongApiClient.stopLive(orgSecret, liveStartDTO.getConverter_id());
//                redisProvider.del(RedisKeyName.SI_KONG_IN_LIVE + ":" + sn + ":" + liveStartDTO.getConverter_id());
//            } catch (Exception e) {
//                log.warn("删除无用直播失败");
//            }
//        }
//    }
}
